﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OfficialWeb.Application;

using OOS.Application.Dish.Dtos;
using OOS.Application.Dish.Services.Interfaces;
using OOS.Application.User.Services;
using OOS.Application.User.Services.Interfaces;
using OOS.Core.Dish;

namespace OOS.Application.Dish.Services
{
    /// <summary>
    /// 菜品口味管理
    /// </summary>
    public class DishFlavorService:IDishFlavorService,ITransient
    {
        private readonly ISqlSugarClient _client;

        public DishFlavorService(ISqlSugarClient client)
        {
            _client = client;
        }

        /// <summary>
        /// 新增菜品口味
        /// </summary>
        /// <param name="dishFlavor"></param>
        /// <param name="dishId"></param>
        /// <returns></returns>
        public bool InsertDishFlavor(DishFlavor dishFlavor,int dishId)
        {
            //判断token中是否有用户数据
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            dishFlavor.DishId = dishId;
            dishFlavor.CreateUser = userId;
            dishFlavor.CreateDate = DateTime.Now;
            dishFlavor.UpdateUser = userId;
            dishFlavor.UpdateDate = DateTime.Now;
            dishFlavor.IsDeleted = false;
            return _client.Insertable(dishFlavor).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据DishId查询菜品口味列表
        /// </summary>
        /// <param name="dishId"></param>
        /// <returns></returns>
        public List<DishFlavor> SelectFlavorListByDishId(int dishId)
        {
            return _client.Queryable<DishFlavor>()
                .Where(df => df.DishId == dishId)
                .Where(df => df.IsDeleted == false)
                .ToList();
        }

        /// <summary>
        /// 根据DishId删除对应口味列表
        /// </summary>
        /// <param name="dishId"></param>
        /// <returns></returns>
        public bool DeleteFlavorListByDishId(int dishId)
        {
            //判断token中是否有用户数据
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            List<DishFlavor> dishFlavors = SelectFlavorListByDishId(dishId);
            if (dishFlavors == null || dishFlavors.Count <= 0) return true;
            for (int i = 0; i < dishFlavors.Count;i++)
            {
                dishFlavors[i].UpdateUser = userId;
                dishFlavors[i].UpdateDate = DateTime.Now;
                dishFlavors[i].IsDeleted = true;
            }
            return _client.Updateable(dishFlavors).ExecuteCommand() > 0;
        }
    }
}
